﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace MulticastTV
{

    /// <summary>
    /// Remux / Recode form
    /// GUI filters out some of impossible or not working remux/recode combinations
    /// </summary>
    public partial class RemuxForm : Form
    {
        static string path = Application.StartupPath;
        Transcode convert;

        /// <summary>
        /// Constructor
        /// </summary>
        public RemuxForm()
        {
            InitializeComponent();
            convert = new Transcode(this);
        }

        private void RemuxForm_Load(object sender, EventArgs e)
        {
           // default file format mpeg ps
            clearFileFormatChkBoxes();
            checkBox1.CheckState = CheckState.Checked;
            populateAVcombobox();

            //deinterlace settings
            if (Properties.Settings.Default.TranscodeDeinterlace)
                checkBox8.CheckState = CheckState.Checked;
            else
                checkBox8.CheckState = CheckState.Unchecked;
        }

        private void greyBitRates()
        {
            if (comboBox1.Items.Count > 0 && comboBox2.Items.Count > 0)
            {
                if (comboBox1.SelectedItem.ToString() == "Original Video")
                {
                    numericUpDown1.Enabled = false;
                    checkBox8.Enabled = false;
                }
                else
                {
                    numericUpDown1.Enabled = true;
                    checkBox8.Enabled = true;
                }

                if (comboBox2.SelectedItem.ToString() == "Original Audio")
                    numericUpDown2.Enabled = false;
                else
                    numericUpDown2.Enabled = true;
            }
        }

        private void populateAVcombobox()
        {    
             // MPEG-PS
             if (checkBox1.CheckState == CheckState.Checked)
               
             {
                // Video codecs
                comboBox1.Items.Clear();
                comboBox1.Items.Add("Original Video");
                comboBox1.Items.Add("MPEG-2");
                comboBox1.SelectedIndex = 0;
               
                //Audio codecs
                comboBox2.Items.Clear();
                comboBox2.Items.Add("Original Audio");
                comboBox2.Items.Add("MPEG-L2");
                comboBox2.Items.Add("MP3");
                comboBox2.Items.Add("AC3");
                comboBox2.SelectedIndex = 0;
            }

             //MPEG2 ts
            if (checkBox2.CheckState == CheckState.Checked)
             {
                 // Video codecs
                 comboBox1.Items.Clear();
                 comboBox1.Items.Add("Original Video");
                 comboBox1.Items.Add("MPEG-2");
                 comboBox1.Items.Add("MPEG-4");
                 comboBox1.Items.Add("H.264");
                 comboBox1.SelectedIndex = 3;

                 //Audio codecs
                 comboBox2.Items.Clear();
                 comboBox2.Items.Add("Original Audio");
                 comboBox2.Items.Add("MPEG-L2");
                 comboBox2.Items.Add("MP3");
                 comboBox2.Items.Add("AC3");
                 comboBox2.Items.Add("MP4A");
                 comboBox2.SelectedIndex = 3;
             }

            //MP4
            else if (checkBox3.CheckState == CheckState.Checked)
             { 
                // Video codecs
                comboBox1.Items.Clear();
                comboBox1.Items.Add("Original Video");
                comboBox1.Items.Add("MPEG-2");
                comboBox1.Items.Add("MPEG-4");
                comboBox1.Items.Add("H.264");
                comboBox1.SelectedIndex = 2;
               
                //Audio codecs
                comboBox2.Items.Clear();
                comboBox2.Items.Add("Original Audio");
                comboBox2.Items.Add("MPEG-L2");
                comboBox2.Items.Add("MP3");
                comboBox2.Items.Add("MP4A");
                comboBox2.SelectedIndex = 2;
             }

            //WMV
            else if (checkBox4.CheckState == CheckState.Checked)
             { 
                // Video codecs
                comboBox1.Items.Clear();
                comboBox1.Items.Add("MPEG-2");
                comboBox1.Items.Add("DIVX3");
                comboBox1.Items.Add("MPEG-4");
                comboBox1.Items.Add("WMV2");
                comboBox1.Items.Add("H.264");
                comboBox1.SelectedIndex = 3;
               
                //Audio codecs
                comboBox2.Items.Clear();
                comboBox2.Items.Add("Original Audio");
                comboBox2.Items.Add("MPEG-L2");
                comboBox2.Items.Add("MP3");
                comboBox2.Items.Add("AC3");
                comboBox2.Items.Add("WMA");
                comboBox2.SelectedIndex = 4;
             }

            //AVI
            else if (checkBox5.CheckState == CheckState.Checked)
             { 
                 // Video codecs
                comboBox1.Items.Clear();
                comboBox1.Items.Add("Original Video");
                comboBox1.Items.Add("MPEG-2");
                comboBox1.Items.Add("DIVX3");
                comboBox1.Items.Add("MPEG-4");
                comboBox1.Items.Add("WMV2");
                comboBox1.Items.Add("H.264");
                comboBox1.SelectedIndex = 2;
               
                //Audio codecs
                comboBox2.Items.Clear();
                comboBox2.Items.Add("Original Audio");
                comboBox2.Items.Add("MPEG-L2");
                comboBox2.Items.Add("MP3");
                comboBox2.Items.Add("AC3");
                comboBox2.Items.Add("WMA");
                comboBox2.SelectedIndex = 2;
             }
             
            //FLV
            else if (checkBox6.CheckState == CheckState.Checked)
             {
                 // Video codecs
                 comboBox1.Items.Clear();
                 comboBox1.Items.Add("FLV1");
                 comboBox1.Items.Add("H.264");
                 comboBox1.SelectedIndex = 0;

                 //Audio codecs
                 comboBox2.Items.Clear();
                 comboBox2.Items.Add("MPEG-L2");
                 comboBox2.Items.Add("MP3");
                 comboBox2.Items.Add("MP4A");
                // comboBox2.Items.Add("AAC");
                 comboBox2.SelectedIndex = 1;
             }
        }

        private string toFourCC(string input)
        {
            if (input == "Original Video")
                return "orig";
            else if (input == "Original Audio")
                return "orig";
            else if (input == "MPEG-2")
                return "mp2v";
            else if (input == "DIVX3")
                return "divx3";
            else if (input == "MPEG-4")
                return "mp4v";
            else if (input == "WMV2")
                return "WMV2";
            else if (input == "H.264")
                return "h264";
            else if (input == "FLV1")
                return "FLV1";

            else if (input == "MPEG-L2")
                return "mp2a";
            else if (input == "MP3")
                return "mp3";
            else if (input == "MP4A")
                return "mp4a";
            else if (input == "AC3")
                return "a52";
            else if (input == "WMA")
                return "wma";
            else if (input == "AAC")
                return "aac";

            else return "";
        }

        private void beginTranscodingFile()
        {
            //check if input file is empty
            if (textBox1.Text.Length == 0)
            {
                if (Properties.Settings.Default.language == "Slovenian")
                    MessageBox.Show("Izberite vhodno datoteko.");
                else
                    MessageBox.Show("Select input file.");
                
                return;
            }

            if (!File.Exists(textBox1.Text))
                return;
            
            if (textBox2.Text.Length > 0 && Path.GetExtension(textBox2.Text)!= string.Empty)
            {
                if (!Path.IsPathRooted(textBox2.Text))
                {
                    textBox2.Text = Path.GetDirectoryName(textBox1.Text) + @"\" + textBox2.Text;
                }
                 
                //check if file in dest directory allready exists
                string[] files = Directory.GetFiles(Path.GetDirectoryName(textBox2.Text));
                string ourfile = textBox2.Text;

                string mux="ts";
                if (checkBox1.CheckState == CheckState.Checked)
                    mux = "ps";
                else if (checkBox2.CheckState == CheckState.Checked)
                    mux = "ts";
                else if (checkBox3.CheckState == CheckState.Checked)
                    mux = "mp4";
                else if (checkBox4.CheckState == CheckState.Checked)
                    mux = "asf";
                else if (checkBox5.CheckState == CheckState.Checked)
                    mux = "avi";
                else if (checkBox6.CheckState == CheckState.Checked)
                    mux = "flv";
                string vcodec = comboBox1.SelectedItem.ToString();
                string acodec = comboBox2.SelectedItem.ToString();
                string vbitrate = numericUpDown1.Value.ToString();
                string abitrate = numericUpDown2.Value.ToString();

                bool duplicate = true;
                if (checkBox7.CheckState == CheckState.Checked)
                    duplicate = true;
                else
                    duplicate = false;

                convert.start(textBox1.Text, ourfile, mux, toFourCC(vcodec), toFourCC(acodec), vbitrate, abitrate, path, duplicate);
            }
        }

        private void clearFileFormatChkBoxes()
        {
            checkBox1.CheckState = CheckState.Unchecked;
            checkBox2.CheckState = CheckState.Unchecked;
            checkBox3.CheckState = CheckState.Unchecked;
            checkBox4.CheckState = CheckState.Unchecked;
            checkBox5.CheckState = CheckState.Unchecked;
            checkBox6.CheckState = CheckState.Unchecked;
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            greyBitRates();
            defaultBitRates();
        }

        private void checkBox5_Click(object sender, EventArgs e)
        {
            clearFileFormatChkBoxes();
            CheckBox s = (CheckBox)sender;
            s.CheckState = CheckState.Checked;
            populateAVcombobox();

            if (checkBox1.CheckState == CheckState.Checked || checkBox2.CheckState==CheckState.Checked)
                changeOutputExt("mpg");
            else if (checkBox3.CheckState == CheckState.Checked)
                changeOutputExt("mp4");
            else if (checkBox4.CheckState == CheckState.Checked)
                changeOutputExt("wmv");
            else if (checkBox5.CheckState == CheckState.Checked)
                changeOutputExt("avi");
            else if (checkBox6.CheckState == CheckState.Checked)
                changeOutputExt("flv");
            }

        /// <summary>
        /// Set default bitrates for particular format
        /// </summary>
        private void defaultBitRates()
        {
            if (comboBox1 != null && comboBox1.Items.Count>0 && comboBox2 != null && comboBox2.Items.Count>0)
            {
                if (comboBox1.SelectedItem.ToString() == "MPEG-2")
                    numericUpDown1.Value = 4000;
                else if (comboBox1.SelectedItem.ToString() == "MPEG-4")
                    numericUpDown1.Value = 1200;
                else if (comboBox1.SelectedItem.ToString() == "DIVX3")
                    numericUpDown1.Value = 1200;

                if (comboBox2.SelectedItem.ToString() == "MPEG-L2")
                    numericUpDown2.Value = 224;
                if (comboBox2.SelectedItem.ToString() == "MP3")
                    numericUpDown2.Value = 128;
                if (comboBox2.SelectedItem.ToString() == "AC3")
                    numericUpDown2.Value = 160;
                if (comboBox2.SelectedItem.ToString() == "AAC")
                    numericUpDown2.Value = 128;
                if (comboBox2.SelectedItem.ToString() == "WMA")
                    numericUpDown2.Value = 128;
            }
       }

        /// <summary>
        /// Change output extension
        /// </summary>
        private void changeOutputExt(string ext)
        {
            if (textBox2.Text.Length > 0)
            {
                try
                {
                    textBox2.Text = Path.ChangeExtension(textBox2.Text,ext);
                }
                catch{ }
            }
        }

        /// <summary>
        /// Input file
        /// </summary>
        private void button3_Click(object sender, EventArgs e)
        {
            openFileDialog1.CheckFileExists = true;
            openFileDialog1.Filter = "mpeg-ts (*.mpg)|*.MPG|All Files|*.*";
            openFileDialog1.FileName = "";
            openFileDialog1.FilterIndex = 1;
            openFileDialog1.InitialDirectory = Properties.Settings.Default.RecFolder;
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                textBox1.Text = openFileDialog1.FileName;
            }
        }

        /// <summary>
        /// Output file
        /// </summary>
        private void button4_Click(object sender, EventArgs e)
        {
            saveFileDialog1.CheckPathExists = true;
            saveFileDialog1.Filter = "mpeg|*.mpg|avi|*.avi|mp4|*.mp4|wmv|*.wmv|flv|*.flv|All Files|*.*";

            if (checkBox1.CheckState == CheckState.Checked ||checkBox2.CheckState ==CheckState.Checked)
            {
                saveFileDialog1.FilterIndex = 1;
                saveFileDialog1.DefaultExt = "mpg";
            }
            else if (checkBox5.CheckState == CheckState.Checked)
            {
                saveFileDialog1.FilterIndex = 2;
                saveFileDialog1.DefaultExt = "avi";
            }
            else if (checkBox3.CheckState == CheckState.Checked)
            {
                saveFileDialog1.FilterIndex = 3;
                saveFileDialog1.DefaultExt = "mp4";
            }
            else if (checkBox4.CheckState == CheckState.Checked)
            {
                saveFileDialog1.FilterIndex = 4;
                saveFileDialog1.DefaultExt = "wmv";
            }

            else if (checkBox6.CheckState == CheckState.Checked)
            {
                saveFileDialog1.FilterIndex = 5;
                saveFileDialog1.DefaultExt = "flv";
            }
            //saveFileDialog1.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
            
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                textBox2.Text = saveFileDialog1.FileName;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            beginTranscodingFile();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            convert.stop();
        }

        private void RemuxForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            convert.stop();
            GC.Collect();
        }

        private void checkBox8_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox8.CheckState == CheckState.Checked)
                Properties.Settings.Default.TranscodeDeinterlace = true;
            else
                Properties.Settings.Default.TranscodeDeinterlace = false;
        }
    }
}
